import React from 'react';

import userStore from '../store/userStore';
import {Row, Col, Form, Card, message} from 'antd';
import LoginForm from '../components/LoginForm';
const Login = Form.create()(LoginForm);

export default class LoginPage extends React.Component {
  static contextTypes = {
    router: React.PropTypes.object
  }

  handleSubmit(user) {
    let router = this.context.router;
    userStore
      .login(user)
      .then((payload) => {
        if (payload.status === 'success') {
          message.success("登入成功！")
          router.push('/');
          return;
        }
        message.warn("登入失败！")
      })
      .catch(() => {
        message.warn("登入失败！")
      });
  }

  render() {
    return (
      <Row
        style={{height: 500}}
        type="flex" justify="center" align="middle">
        <Col>
          <Card style={{width: 350}}>
            <Login handleSubmit={this.handleSubmit.bind(this)} />
          </Card>
        </Col>
      </Row>
    );
  }
}
